<template>
  <div class="app-container">
    <el-form :model="formInline" ref="form" label-width="90px" class="form-inline" label-suffix="：">
      <el-row :gutter="10">
        <el-col :lg="6" :xl="4"><el-form-item label="保单号">
            <el-input v-model="formInline.insureNum" placeholder="请输入保单号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="被保险人">
            <el-input v-model="formInline.insuredName" placeholder="请输入被保险人"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="订单号">
            <el-input v-model="formInline.orderNum" placeholder="请输入订单号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="报案号">
            <el-input v-model="formInline.reportNum" placeholder="请输入报案号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="箱号">
            <el-input v-model="formInline.containerNumber" placeholder="请输入箱号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="运单号">
            <el-input v-model="formInline.tagInvoiceWaybill" placeholder="请输入运单号"></el-input> </el-form-item></el-col>
        <el-col :lg="6" :xl="4"><el-form-item label="当前节点">
            <el-select v-model="formInline.reportStatus" placeholder="请选择当前节点">
              <el-option label="全部" value=""></el-option>
              <el-option v-for="item in ReportStatus" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select> </el-form-item></el-col>
        <el-col :lg="6" :xl="6">
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button type="primary" :disabled="!tableData.length" @click="downloadExcel">导出</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table ref="table" :data="tableData">
      <el-table-column align="center" prop="insureNum" label="保单号" width="180">
      </el-table-column>
      <el-table-column align="center" prop="reportTime" label="出险日期" width="180">
      </el-table-column>
      <el-table-column align="center" prop="insuredName" label="被保险人" width="180">
      </el-table-column>
      <el-table-column align="center" prop="reportNum" label="报案号" width="180">
        <template slot-scope="scope">
          {{ scope.row.reportNum }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="goodsName" label="货物名称" width="180">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.goodsName }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="containerNumber" label="出险箱号" width="180">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.containerNumber }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="lossFee" label="损失金额">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.lossFee }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="sceneName" label="现场联系人" width="100">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.sceneName }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="scenePhone" label="联系方式" width="140">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.scenePhone }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="reportStatus" label="当前节点">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportStatus === '0'">未立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '1'" type="warning">报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '2'" type="warning">立案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '3'" type="danger">取消报案</el-tag>
          <el-tag v-if="scope.row.reportStatus === '4'" type="warning">勘察/定损</el-tag>
          <el-tag v-if="scope.row.reportStatus === '5'" type="warning">核赔</el-tag>
          <el-tag v-if="scope.row.reportStatus === '6'" type="success">付款</el-tag>
          <el-tag v-if="scope.row.reportStatus === '7'" type="success">结案</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="reportPlace" label="出险地址">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.reportPlace }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="reportCause" label="出险原因">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.reportCause }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="explain" label="损失情况说明">
        <template slot-scope="scope">
          {{ scope.row.frontReportInsure.explain }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="view(scope.row)">查看</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus == 1" type="text" icon="el-icon-view" @click="confirmFiling(scope.row)">确认立案</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus != 7" type="text" icon="el-icon-error" @click="cancelFiling(scope.row)">取消立案</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus == 3" type="text" icon="el-icon-document-delete" @click="confirmCancelFiling(scope.row)">确认取消立案</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus == 4" type="text" icon="el-icon-document-checked" @click="confirmVerification(scope.row)">确认核赔</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus == 4" type="text" icon="el-icon-folder-delete" @click="cancelVerification(scope.row)">核验失败</el-button>
          <el-button size="mini" v-if="scope.row.reportStatus == 5" type="text" icon="el-icon-s-claim" @click="confirmPayment(scope.row)">确认付款</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-document" @click="followingClick(item.orderNum)">跟踪</el-button> -->
          <!-- <el-button v-if="scope.row.status !== '通过'" size="mini" type="text" icon="el-icon-document-checked"
                        @click="examine(scope.row)">审核</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination style="margin-top: 20px" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.pageNum" :page-sizes="[10, 20, 50]"
      :page-size="page.pageSize" layout="->, total, sizes, prev, pager, next, jumper" :total="page.total">
    </el-pagination>
    <view-or-examine ref="viewOrExamine"></view-or-examine>
    <el-dialog title="确认立案" :visible.sync="showConfirmFiling" :label-width="150" v-loading="loading">
      <el-form ref="confirmFilingForm" :rules="rules" :model="confirmFilingForm" label-width="150px">
        <el-form-item label="报案号" prop="reportNum">
          <el-input v-model="confirmFilingForm.reportNum"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="confirmFilingForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showConfirmFiling = false">取 消</el-button>
        <el-button type="primary" @click="confirmFilingFunc">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="确认取消立案" :visible.sync="showConfirmCancelFiling" v-loading="loading">
      <el-form ref="confirmCancelFilingForm" :rules="rules" :model="confirmCancelFilingForm" label-width="150px">
        <el-form-item label="备注" prop="remark" :rules="[{ required: true, message: '请输入备注', trigger: 'blur' }]">
          <el-input v-model="confirmCancelFilingForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showConfirmCancelFiling = false">取 消</el-button>
        <el-button type="primary" @click="confirmCancelFilingFunc">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="确认核赔" :visible.sync="showCompensation" v-loading="loading">
      <el-form ref="confirmCompensationForm" :rules="rulesCompensation" :model="confirmCompensationForm" label-width="150px">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="confirmCompensationForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCompensation = false">取 消</el-button>
        <el-button type="primary" @click="confirmVerificationFun">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="取消立案" :visible.sync="showCancelFiling" v-loading="loading">
      <el-form :model="cancelFilingForm" ref="cancelFilingForm" label-width="150px">
        <el-form-item label="取消原因备注" prop="remark" :rules="[
      { required: true, message: '请输入取消原因', trigger: 'blur' },
    ]">
          <el-input type="textarea" v-model="cancelFilingForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCancelFiling = false">取 消</el-button>
        <el-button type="primary" @click="cancelFilingFunc">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="核验失败" :visible.sync="showCancelVerification" v-loading="loading">
      <el-form :model="cancelVerificationForm" ref="cancelVerificationForm" label-width="150px">
        <el-form-item label="备注" prop="remark" :rules="[{ required: true, message: '请输入备注', trigger: 'blur' }]">
          <el-input type="textarea" v-model="cancelVerificationForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showCancelVerification = false">取 消</el-button>
        <el-button type="primary" @click="cancelVerificationFunc">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="确认付款" :visible.sync="showConfirmPayment">
      <el-form :model="confirmPaymentForm" ref="confirmPaymentForm" label-width="150px">
        <el-form-item label="赔付金额" prop="reportPayMoney" :rules="[
      { required: true, message: '请输入赔付金额', trigger: 'blur' },
      {
        validator: (rule, value, cb) => checkNum(rule, value, cb),
        message: '请输入数字',
      },
    ]">
          <el-input type="textarea" v-model="confirmPaymentForm.reportPayMoney"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="confirmPaymentForm.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showConfirmPayment = false">取 消</el-button>
        <el-button type="primary" @click="confirmPaymentFunc">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getLabel } from "@/utils/func.js";
import viewOrExamine from "./viewOrExamine.vue";
import { ReportStatus } from "@/utils/options";
import moment from "moment";
const reg5 = /^\d*\.?\d+$/;
export default {
  components: { viewOrExamine },
  data() {
    return {
      loading: false,
      showCancelVerification: false,
      cancelVerificationForm: {
        remark: "",
      },

      showConfirmCancelFiling: false,
      confirmCancelFilingForm: {
        remark: "",
      },
      showConfirmPayment: false,
      confirmPaymentForm: {
        reportPayMoney: "",
        remark: "",
      },
      showCancelFiling: false,
      cancelFilingForm: {
        remark: "",
      },
      showCompensation: false,
      showConfirmFiling: false,
      getLabel,
      ReportStatus,
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      currentPage4: 4,
      formInline: {
        insureNum: "",
        orderNum: "",
        reportNum: "",
        containerNumber: "",
        tagInvoiceWaybill: "",
        insuredName: "",
        reportStatus: "",
      },
      tableData: [],
      tableHeight: 0,
      confirmFilingForm: {
        reportNum: "",
        remark: "",
      },
      row: {},
      rules: {
        reportNum: [
          { required: true, message: "请输入报案号", trigger: "blur" },
        ],
      },
      rulesCompensation: {
        reportPayMoney: [
          { required: true, message: "请输入赔付金", trigger: "blur" },
        ],
      },
      confirmCompensationForm: {
        reportPayMoney: "",
      },
    };
  },
  methods: {
    downloadExcel() {
      const params = {
        ...this.formInline,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      const headers = {
        responseType: "blob",
      };
      // 发送请求获取Excel文件流
      const date = moment().format("YYYY-MM-DD HH:mm:ss");
      this.download(
        "/warranty/report/exportClaim",
        {
          ...this.params,
        },
        `出险保单信息${date}.xls`
      );
    },
    checkNum(rule, value, cb) {
      if (!reg5.test(value)) {
        cb("请输入数字");
      }
      cb();
    },
    cancelVerificationFunc() {
      this.$refs.cancelVerificationForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            remark: this.cancelVerificationForm.remark,
          };

          this.$post(`/warranty/report/cancelCheck`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showCancelVerification = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    cancelFilingFunc() {
      this.$refs.cancelFilingForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            remark: this.cancelFilingForm.remark,
          };
          this.$post(`/warranty/report/cancelReport`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showCancelFiling = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    confirmPaymentFunc() {
      this.$refs.confirmPaymentForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            reportPayMoney: this.confirmPaymentForm.reportPayMoney,
            remark: this.confirmPaymentForm.remark,
          };
          this.$post(`/warranty/report/confirmPay`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showConfirmPayment = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    confirmCancelFiling(row) {
      this.confirmCancelFilingForm = {
        remark: "",
      };
      this.row = row;
      this.showConfirmCancelFiling = true;
    },
    confirmFiling(row) {
      this.confirmFilingForm = {
        reportNum: "",
        remark: "",
      };
      this.row = row;
      this.showConfirmFiling = true;
    },
    confirmFilingFunc() {
      this.$refs.confirmFilingForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            reportNum: this.confirmFilingForm.reportNum,
            remark: this.confirmFilingForm.remark,
          };
          this.$post(`/warranty/report/confirmReport`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showConfirmFiling = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    cancelFiling(row) {
      this.cancelFilingForm = {
        remark: "",
      };
      this.row = row;
      this.showCancelFiling = true;
    },
    confirmCancelFilingFunc() {
      this.$refs.confirmCancelFilingForm.validate((valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          this.loading = true;
          const params = {
            orderNum: this.row.orderNum,
            remark: this.confirmCancelFilingForm.remark,
          };
          this.$post(`/warranty/report/confirmcancelReport`, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.showConfirmCancelFiling = false;
                this.getTableData();
              }
            })
            .finally(() => {
              this.loading = false;
            });
        }
      });
    },
    confirmVerification(row) {
      this.confirmCompensationForm = {
        remark: "",
      };
      this.showCompensation = true;
      this.row = row;
    },
    confirmVerificationFun() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      const params = {
        orderNum: this.row.orderNum,
        remark: this.confirmCompensationForm.remark,
      };
      this.$post(`/warranty/report/confirmCheck`, params)
        .then((res) => {
          if (res && res.code * 1 === 200) {
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            this.showCompensation = false;
            this.getTableData();
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    cancelVerification(row) {
      this.cancelVerificationForm = {
        remark: "",
      };
      this.row = row;
      this.showCancelVerification = true;
    },
    confirmPayment(row) {
      this.confirmPaymentForm = {
        reportPayMoney: "",
        remark: "",
      };
      this.row = row;
      this.showConfirmPayment = true;
    },
    onSubmit() {
      this.page.pageNum = 1;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getTableData();
    },
    view(row) {
      this.$refs.viewOrExamine.dialogFormVisible = true;
      this.$refs.viewOrExamine.title = "查看";
      this.$refs.viewOrExamine.row = row;
      this.$refs.viewOrExamine.followingClick(row.orderNum);
      this.$refs.viewOrExamine.isExamine = false;
    },
    examine() {
      this.$refs.viewOrExamine.dialogFormVisible = true;
      this.$refs.viewOrExamine.title = "审核";
      this.$refs.viewOrExamine.isExamine = true;
    },
    setTableHeight() {
      this.$nextTick(() => {
        const queryConditionHeight = this.$refs.form.$el.offsetHeight;
        this.tableHeight =
          window.innerHeight - queryConditionHeight - 180 + "px";
      });
    },
    getTableData() {
      const params = {
        ...this.formInline,
        pageNum: this.page.pageNum,
        pageSize: this.page.pageSize,
      };
      this.$get("warranty/report/list", params).then((res) => {
        this.tableData = res.rows;
        this.page.total = res.total;
      });
    },
  },
  mounted() {
    this.getTableData();
    this.setTableHeight();
  },
};
</script>
<style scoped lang="scss">
.form-inline ::v-deep {
  width: 100%;

  .el-form-item__label {
    // padding: 0;
  }
}
</style>
